package com.heima.tess4j;

import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


public class Application {
    public static void main(String[] args) {
        try {
            // 获取本地图片
            File file = new File("C:\\Users\\asus\\Desktop\\Screenshot 2024-09-20 204625.jpg");
            if (!file.exists() || !file.canRead()) {
                System.err.println("图像文件不存在或无法读取");
                return;
            }

            // 创建Tesseract对象
            Tesseract tesseract = new Tesseract();
            // 设置字体库路径
            tesseract.setDatapath("E:\\hmtoutiao\\day02\\tessdata");
            // 中文识别
            tesseract.setLanguage("chi_sim");
            // 执行ocr识别
            String result = tesseract.doOCR(file);
            // 替换回车和tab键 使结果为一行
            result = result.replaceAll("\\r|\\n", "-").replaceAll(" ", "");
            System.out.println("识别的结果为：" + result);
        } catch (TesseractException e) {
            System.err.println("Tesseract异常: " + e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            System.err.println("其他异常: " + e.getMessage());
            e.printStackTrace();
        }
    }
}


